"use strict";
this.AutoFormSubmit = function () {};

this.AutoFormSubmit.prototype = (function () {
    var init, thread, onConfigurationLoaded, forms, areas, initForm, onRedraw,
            initInputs, initRadios, initCheckboxes, initSelects, initTextareas;

    init = function () {
        areas = [];
        forms = [];
    };

    thread = function () {};

    onConfigurationLoaded = function (webLayout, config) {
        var i, j, forms;
        if (config[this.NAME] === undefined) {
            return;
        }
        areas = config[this.NAME];
        for (i = areas.length - 1; i >= 0; i -= 1) {
            forms = webLayout.xPath(areas[i].forms);
            for (j = forms.length - 1; j >= 0; j -= 1) {
                initForm(forms[j], areas[i].fields, webLayout);
            }
        }
    };

    onRedraw = function (webLayout, details) {
        var i, j, k, newForms, isNew;
        for (i = areas.length - 1; i >= 0; i -= 1) {
            newForms = webLayout.xPath(areas[i].forms);
            for (j = newForms.length - 1; j >= 0; j -= 1) {
                isNew = true;
                for (k = forms.length - 1; k >= 0; k -= 1) {
                    if (newForms[j] === forms[k]) {
                        isNew = false;
                    }
                }
                if (isNew) {
                    initForm(newForms[j], areas[i].fields, webLayout);
                }
            }
        }
    };

    initForm = function (form, fields, webLayout) {
        var i;
        for (i = fields.length - 1; i >= 0; i -= 1) {
            if (fields[i] === 'input') {
                initInputs(form, webLayout);
            } else if (fields[i] === 'radio') {
                initRadios(form, webLayout);
            } else if (fields[i] === 'checkbox') {
                initCheckboxes(form, webLayout);
            } else if (fields[i] === 'select') {
                initSelects(form, webLayout);
            } else if (fields[i] === 'textarea') {
                initTextareas(form, webLayout);
            }
        }
        forms.push(form);
    };

    initTextareas = function (form, webLayout) {
        var fields, i;
        fields = webLayout.xPath("..//textarea", form);
        for (i = fields.length - 1; i >= 0; i -= 1) {
            webLayout.addEventListener(fields[i], 'blur', function () {
                if (webLayout.dispatchEvent(form, 'submit')) {
                    form.submit();
                }
            });
        }
    };

    initSelects = function (form, webLayout) {
        var fields, i;
        fields = webLayout.xPath(".//select", form);
        for (i = fields.length - 1; i >= 0; i -= 1) {
            webLayout.addEventListener(fields[i], 'change', function () {
                if (webLayout.dispatchEvent(form, 'submit')) {
                    form.submit();
                }
            });
        }
    };

    initCheckboxes = function (form, webLayout) {
        var fields, i;
        fields = webLayout.xPath(".//input[@type='checkbox']", form);
        for (i = fields.length - 1; i >= 0; i -= 1) {
            webLayout.addEventListener(fields[i], 'click', function () {
                if (webLayout.dispatchEvent(form, 'submit')) {
                    form.submit();
                }
            });
        }
    };

    initRadios = function (form, webLayout) {
        var fields, i;
        fields = webLayout.xPath(".//input[@type = 'radio']", form);
        for (i = fields.length - 1; i >= 0; i -= 1) {
            webLayout.addEventListener(fields[i], 'click', function () {
                if (webLayout.dispatchEvent(form, 'submit')) {
                    form.submit();
                }
            });
        }
    };

    initInputs = function (form, webLayout) {
        var fields, i;
        fields = webLayout.xPath(".//input[(@type != 'radio') and " +
                "(@type != 'checkbox')]", form);
        for (i = fields.length - 1; i >= 0; i -= 1) {
            webLayout.addEventListener(fields[i], 'blur', function () {
                if (webLayout.dispatchEvent(form, 'submit')) {
                    form.submit();
                }
            });
        }
    };

    return {
        NAME: 'AutoFormSubmit',
        init: init,
        thread: thread,
        onConfigurationLoaded: onConfigurationLoaded,
        onRedraw: onRedraw
    };
}());